/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Views;

import Common.ChagesLanguage;
import Common.ConnectionDB;
import Common.Logins;
import java.awt.Toolkit;
import java.io.File;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author TrongChien
 */
public class Changepassword extends javax.swing.JDialog {

    Connection cnn = ConnectionDB.ConnectionDatabase();
    Statement st = null;
    ResultSet rs = null;
    private ResourceBundle rb;
    

    /**
     * Creates new form Changepassword
     */
    public Changepassword(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        initChangepassword();
    }

    public void initChangepassword() {
         ChagesLanguage.readFileLanguage();
          Locale locale = new Locale(ChagesLanguage.languageCode, ChagesLanguage.countryCode);
        rb = ResourceBundle.getBundle(ChagesLanguage.urlLanguage, locale);
        lblUsername.setText(rb.getString("Username"));
        lblPasswordOld.setText(rb.getString("Password"));
        lblNewPassword.setText(rb.getString("NewPassword"));
        lblConfirmPassword.setText(rb.getString("ConfirmPassword"));
        this.setTitle(rb.getString("Changepassword"));
        bntChangePassword.setText(rb.getString("Change"));
        bntCancel.setText(rb.getString("Cancel"));
        txtUsername.setText(Logins.getUserName());
        this.setLocationRelativeTo(null);
        try {
            setIconImage(Toolkit.getDefaultToolkit().getImage(new File("src\\Images\\Icon-Form.png").getCanonicalPath()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblUsername = new javax.swing.JLabel();
        txtUsername = new javax.swing.JTextField();
        lblPasswordOld = new javax.swing.JLabel();
        lblNewPassword = new javax.swing.JLabel();
        lblConfirmPassword = new javax.swing.JLabel();
        bntChangePassword = new javax.swing.JButton();
        bntCancel = new javax.swing.JButton();
        txtPasswordOld = new javax.swing.JPasswordField();
        txtNewPassword = new javax.swing.JPasswordField();
        txtConfirmPassword = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        lblUsername.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblUsername.setText("Username:");

        txtUsername.setEditable(false);
        txtUsername.setMinimumSize(new java.awt.Dimension(6, 30));

        lblPasswordOld.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblPasswordOld.setText("Password Old:");

        lblNewPassword.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblNewPassword.setText("New Password:");

        lblConfirmPassword.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblConfirmPassword.setText("Confirm Password:");

        bntChangePassword.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/icon-update.png"))); // NOI18N
        bntChangePassword.setText("jButton1");
        bntChangePassword.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        bntChangePassword.setPreferredSize(new java.awt.Dimension(100, 30));
        bntChangePassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntChangePasswordActionPerformed(evt);
            }
        });

        bntCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/icon-cancel.png"))); // NOI18N
        bntCancel.setText("jButton1");
        bntCancel.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        bntCancel.setPreferredSize(new java.awt.Dimension(100, 30));
        bntCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblNewPassword, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblPasswordOld, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblUsername, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblConfirmPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtUsername, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtPasswordOld)
                    .addComponent(txtNewPassword)
                    .addComponent(txtConfirmPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(bntChangePassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(bntCancel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(62, 62, 62))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblUsername)
                    .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPasswordOld)
                    .addComponent(txtPasswordOld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNewPassword)
                    .addComponent(txtNewPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblConfirmPassword)
                    .addComponent(txtConfirmPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bntChangePassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bntCancel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(26, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bntCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntCancelActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_bntCancelActionPerformed

    private void bntChangePasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntChangePasswordActionPerformed
        // TODO add your handling code here:
        if (!Common.CheckForm.isPwdUsername(txtPasswordOld.getText())) {
            JOptionPane.showMessageDialog(this, rb.getString("Pleaseenteroldpassword"), rb.getString("Warning"),JOptionPane.WARNING_MESSAGE);
            txtPasswordOld.requestFocus();
            return;
        }
        if (!Common.CheckForm.isPwdUsername(txtNewPassword.getText())) {
            JOptionPane.showMessageDialog(this, rb.getString("PleaseenterNewpassword"), rb.getString("Warning"),JOptionPane.WARNING_MESSAGE);
            txtNewPassword.requestFocus();
            return;
        }

        if (!Common.CheckForm.isPwdUsername(txtConfirmPassword.getText())) {
            JOptionPane.showMessageDialog(this, rb.getString("PleaseenterComfirmpassword"), rb.getString("Warning"),JOptionPane.WARNING_MESSAGE);
            txtConfirmPassword.requestFocus();
            return;
        }
        if (!txtNewPassword.getText().equalsIgnoreCase(txtConfirmPassword.getText())) {
            JOptionPane.showMessageDialog(this, rb.getString("PasswordvsConfirmpasswordmustbethesame"), rb.getString("Warning"),JOptionPane.WARNING_MESSAGE);
            txtConfirmPassword.selectAll();
            txtConfirmPassword.requestFocus();
            return;
        }
        String strUsername = txtUsername.getText();
        String srtPasswordOld = txtPasswordOld.getText();
        String strPasswordNew = txtNewPassword.getText();
        String strpass = srtPasswordOld;
        String sql = "select * from tblUsers where Username='" + strUsername + "'";
        try {
            st = cnn.createStatement();
            rs = st.executeQuery(sql);
            String dataPass = null;
            rs.next();
            dataPass = rs.getString("Password");
            if (!strpass.equals(dataPass)) {
                JOptionPane.showMessageDialog(this, rb.getString("Oldpasswordincorrectly") + ".", rb.getString("Warning"),JOptionPane.WARNING_MESSAGE);
                txtPasswordOld.selectAll();
                txtPasswordOld.requestFocus();
                return;
            }
            else{
                String sqlupdate = "update tblUsers set Password='"+strPasswordNew+"' where Username='"+strUsername+"'";
                 st = cnn.createStatement();
                 st.executeUpdate(sqlupdate);
                 JOptionPane.showMessageDialog(this, rb.getString("Changepasswordsuccessfully") + ".", rb.getString("Warning"),JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Changepassword.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_bntChangePasswordActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bntCancel;
    private javax.swing.JButton bntChangePassword;
    private javax.swing.JLabel lblConfirmPassword;
    private javax.swing.JLabel lblNewPassword;
    private javax.swing.JLabel lblPasswordOld;
    private javax.swing.JLabel lblUsername;
    private javax.swing.JPasswordField txtConfirmPassword;
    private javax.swing.JPasswordField txtNewPassword;
    private javax.swing.JPasswordField txtPasswordOld;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables
}
